//
//  JSLoadingView.m
//  NiubilityProject
//
//  Created by Apple on 2018/3/6.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSLoadingView.h"

static NSArray *js_refrenshImages = nil;

@implementation JSLoadingView

+ (instancetype)loadViewWithFrame:(CGRect)frame{
    JSLoadingView *loadView = [[JSLoadingView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    loadView.style = JSLoadingCycle;
    return loadView;
}


- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [UIImageView new];
        UIImage *image = nil;
        if (self.style == JSLoadingCycle) {
            image = UIImageMake(@"loading");
        }else{
            image = [UIImage imageNamed:@"loadingcai2new001"];
        }
        _imageView.size = CGSizeMake(image.size.width, image.size.height);
        _imageView.left = (self.width - _imageView.size.width) / 2.0;
        _imageView.top = (self.height - _imageView.size.height) / 2.0 - _imageView.size.height/4;
        [self addSubview:_imageView];
    }
    return _imageView;
}
- (instancetype) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = JS_colorWithHex(0xe7e7e7);
        [self addAnimalImages];
    }
    return self;
}
- (NSArray *)refreshingImages{
    if (!js_refrenshImages) {
        NSMutableArray *refreshingImages  = [NSMutableArray array];
        for (int i = 1; i < 25; ++i) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loadingcai2new00%d",i]];
            [refreshingImages addObject:image];
        }
        js_refrenshImages = [refreshingImages copy];
    }
    return js_refrenshImages;
}
- (void)addAnimalImages{
    self.imageView.animationDuration = 1.5;
    self.imageView.animationImages = [self refreshingImages];
}
+ (void)showLoadingInView:(UIView *)view{
    [self showLoadingInview:view edgeInsets:UIEdgeInsetsZero];
}

+ (void)showLoadingInview:(UIView *)view edgeInsets:(UIEdgeInsets)edgeInsets{
    JSLoadingView *loadingView = [self loadViewWithFrame:view.frame];
    
    loadingView.edgeInsets = edgeInsets;
    
    [loadingView showInView:view];
}
+ (void)showLoadingInView:(UIView *)view topEdge:(CGFloat)topEdge{
    JSLoadingView *loadingView = [self loadViewWithFrame:view.frame];
    loadingView.edgeInsets = UIEdgeInsetsMake(topEdge, 0, 0, 0);
    [loadingView showInView:view];
}

+ (void)hidenLoadingForView:(UIView *)view{
    JSLoadingView *loadingView = [self loadingForView:view];
    if (loadingView) {
        [loadingView hiden];
    }
}

+ (void)hidenAllLoadingForView:(UIView *)view{
    NSEnumerator *reverseSubviews = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in reverseSubviews) {
        if ([subview isKindOfClass:self]) {
            [(JSLoadingView *)subview hideNoAnimation];
        }
    }
}

+ (JSLoadingView *)loadingForView:(UIView *)view{
    NSEnumerator *reverseSubviews = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in reverseSubviews) {
        if ([subview isKindOfClass:self]) {
            return (JSLoadingView *)subview;
        }
    }
    return nil;
}

#pragma mark - 实例方法
- (void)showInView:(UIView *)view{
    if (!view) {
        return ;
    }
    if (self.superview != view) {
        [self removeFromSuperview];
        [view addSubview:self];
        [view bringSubviewToFront:self];
    }
    if (self.style == JSLoadingCycle) {
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        rotationAnimation.duration = 2.0;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = MAXFLOAT;
        [self.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }else{
        [self.imageView startAnimating];
    }
}

- (void)hiden{
    if (self.style == JSLoadingCycle) {
        [self.imageView.layer removeAllAnimations];
    }else{
        self.alpha = 1.0;
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 0;
        }completion:^(BOOL finished) {
            [self hideNoAnimation];
        }];
    }

}

static UIImageView * extracted(JSLoadingView *object) {
    return object.imageView;
}

- (void)hideNoAnimation{
    [extracted(self) stopAnimating];
    [self removeFromSuperview];
}


- (void)dealloc{
    NSLog(@"%@释放了",[self class]);
}
@end
